package com.teamcenter.rac.massupdate.model;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.beans.BeansObservables;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.core.databinding.observable.value.WritableValue;

import com.teamcenter.rac.kernel.TCComponent;
@SuppressWarnings({"unused","rawtypes","unchecked"})
public class SubmitToWorkflowDialogModel<T extends ISubmitToWorkflowRow> extends AbstractWrappedPropertySource {
	List<T> m_rows;
	public static final String PROP_SOMETHING_SELECTED = "somethingSelected";
	private int selectCount;
	private static final int NUM_LOCAL_PROP = 1;
	private DataBindingContext m_bindingContext = new DataBindingContext();

	public SubmitToWorkflowDialogModel(List<T> paramList) {
		super(1);
		this.m_localPropNames.add("somethingSelected");
		this.m_rows = paramList;
		this.selectCount = this.m_rows.size();
		initDataBindings();
	}

	public List<T> getRows() {
		return this.m_rows;
	}

	public List<TCComponent> getSelectedComponents() {
		ArrayList localArrayList = new ArrayList();
		Iterator localIterator = this.m_rows.iterator();
		while (localIterator.hasNext()) {
			ISubmitToWorkflowRow localISubmitToWorkflowRow = (ISubmitToWorkflowRow) localIterator.next();
			Object localObject = localISubmitToWorkflowRow.getValue("selected");
			if (Boolean.TRUE.equals(localObject)) {
				localArrayList.add(localISubmitToWorkflowRow.getComponent());
			}
		}
		return localArrayList;
	}

	public void initDataBindings() {
		Iterator localIterator = this.m_rows.iterator();
		while (localIterator.hasNext()) {
			ISubmitToWorkflowRow localISubmitToWorkflowRow = (ISubmitToWorkflowRow) localIterator.next();
			IObservableValue localIObservableValue = BeansObservables.observeValue(localISubmitToWorkflowRow,
					"selected");
			this.m_bindingContext.bindValue(new IC_SomethingSelectedObservable(), localIObservableValue, null, null);
		}
	}

	public void setSomethingSelected(boolean paramBoolean) {
		setPropertyValue("somethingSelected", Boolean.valueOf(paramBoolean));
	}

	public boolean isSomethingSelected() {
		return getPropertyValueAsBoolean("somethingSelected");
	}

	class IC_SomethingSelectedObservable extends WritableValue {
		IC_SomethingSelectedObservable() {
		}

		public void doSetValue(Object paramObject) {
			if (paramObject == null) {
				return;
			}
			if (((Boolean) paramObject).booleanValue()) {
				SubmitToWorkflowDialogModel.this.selectCount += 1;
			} else {
				SubmitToWorkflowDialogModel.this.selectCount -= 1;
			}
			SubmitToWorkflowDialogModel.this.setSomethingSelected(SubmitToWorkflowDialogModel.this.selectCount > 0);
		}
	}
}